package com.leyou.favorite.service;

import com.leyou.common.auth.pojo.UserHolder;
import com.leyou.common.utils.BeanHelper;
import com.leyou.favorite.Favorite;
import com.leyou.favorite.FavoriteDto;
import com.leyou.favorite.mapper.FavoriteMapper;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.entity.Sku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class FavoriteService {

    @Autowired(required = false)
    private FavoriteMapper favoriteMapper;

    public void addFavorite(Favorite favorite) {
        FavoriteDto favoriteDto=new FavoriteDto();
        //得到当前用户id
        Long userId = UserHolder.getUserId();
        favoriteDto.setUserId(userId);
        favoriteDto.setSkuId(Long.valueOf(favorite.getSkuId()));
        favoriteDto.setTitle(favorite.getTitle());
        favoriteDto.setImage(favorite.getImage());
        favoriteDto.setPrice(favorite.getPrice());
        Long spuId=favoriteMapper.findSpuIdBySkuId(favorite.getSkuId());
        favoriteDto.setSpuId(spuId);
        favoriteMapper.insert(favoriteDto);
    }

    public void deleteFavorite(Long skuId) {
        Long userId = UserHolder.getUserId();
        favoriteMapper.deleteBySkuId(userId,skuId);
    }

    public Integer findFavoriteStatus(Long skuId) {
        Long userId = UserHolder.getUserId();
        Integer state=favoriteMapper.selectBySkuId(userId,skuId);
        if(state==null){
            return 1;
        }else{
            return 0;
        }
    }

    public List<FavoriteDto> findAllFavorite() {
        Long userId = UserHolder.getUserId();
        List<FavoriteDto> favoriteList=favoriteMapper.findAllFavorite(userId);
        return favoriteList;
    }
}
